{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example Seldon Core Deployments using Helm\n",
    "<img src=\"images/deploy-graph.png\" alt=\"predictor with canary\" title=\"ml graph\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prerequisites\n",
    "You will need\n",
    " - [Git clone of Seldon Core](https://github.com/SeldonIO/seldon-core)\n",
    " - A running Kubernetes cluster with kubectl authenticated\n",
    " - [seldon-core Python package](https://pypi.org/project/seldon-core/) (```pip install seldon-core>=0.2.6.1```)\n",
    " - [Helm client](https://helm.sh/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a Kubernetes Cluster\n",
    "\n",
    "Follow the [Kubernetes documentation to create a cluster](https://kubernetes.io/docs/setup/).\n",
    "\n",
    "Once created ensure ```kubectl``` is authenticated against the running cluster."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "namespace/seldon created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create namespace seldon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Context \"kubernetes-admin@kind\" modified.\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl config set-context $(kubectl config current-context) --namespace=seldon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "clusterrolebinding.rbac.authorization.k8s.io/kube-system-cluster-admin created\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl create clusterrolebinding kube-system-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Install Helm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "serviceaccount/tiller created\n",
      "clusterrolebinding.rbac.authorization.k8s.io/tiller created\n",
      "$HELM_HOME has been configured at /home/clive/.helm.\n",
      "\n",
      "Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.\n",
      "\n",
      "Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.\n",
      "To prevent this, run `helm init` with the --tiller-tls-verify flag.\n",
      "For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation\n",
      "Happy Helming!\n"
     ]
    }
   ],
   "source": [
    "!kubectl -n kube-system create sa tiller\n",
    "!kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller\n",
    "!helm init --service-account tiller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting for deployment \"tiller-deploy\" rollout to finish: 0 of 1 updated replicas are available...\n",
      "deployment \"tiller-deploy\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/tiller-deploy -n kube-system"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Start seldon-core"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   seldon-core\n",
      "LAST DEPLOYED: Fri Sep 27 16:50:15 2019\n",
      "NAMESPACE: seldon-system\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1/ClusterRole\n",
      "NAME                     AGE\n",
      "seldon-manager-css-role  0s\n",
      "seldon-manager-role      0s\n",
      "seldon-manager-sas-role  0s\n",
      "seldon-proxy-role        0s\n",
      "\n",
      "==> v1/ClusterRoleBinding\n",
      "NAME                            AGE\n",
      "seldon-manager-css-rolebinding  0s\n",
      "seldon-manager-rolebinding      0s\n",
      "seldon-manager-sas-rolebinding  0s\n",
      "seldon-proxy-rolebinding        0s\n",
      "\n",
      "==> v1/ConfigMap\n",
      "NAME                     DATA  AGE\n",
      "seldon-config            1     1s\n",
      "seldon-spartakus-config  1     1s\n",
      "\n",
      "==> v1/Deployment\n",
      "NAME                       READY  UP-TO-DATE  AVAILABLE  AGE\n",
      "seldon-controller-manager  0/1    1           0          0s\n",
      "\n",
      "==> v1/Pod(related)\n",
      "NAME                                         READY  STATUS             RESTARTS  AGE\n",
      "seldon-controller-manager-864f5f6cb6-mndn5   0/1    ContainerCreating  0         0s\n",
      "seldon-spartakus-volunteer-7d6dd98f89-596rt  0/1    ContainerCreating  0         0s\n",
      "\n",
      "==> v1/Role\n",
      "NAME                         AGE\n",
      "seldon-leader-election-role  0s\n",
      "seldon-manager-cm-role       0s\n",
      "\n",
      "==> v1/RoleBinding\n",
      "NAME                                AGE\n",
      "seldon-leader-election-rolebinding  0s\n",
      "seldon-manager-cm-rolebinding       0s\n",
      "\n",
      "==> v1/Secret\n",
      "NAME                        TYPE               DATA  AGE\n",
      "seldon-webhook-server-cert  kubernetes.io/tls  3     1s\n",
      "\n",
      "==> v1/Service\n",
      "NAME                                       TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)   AGE\n",
      "seldon-controller-manager-metrics-service  ClusterIP  10.96.193.209   <none>       8443/TCP  0s\n",
      "seldon-webhook-service                     ClusterIP  10.100.190.225  <none>       443/TCP   0s\n",
      "\n",
      "==> v1/ServiceAccount\n",
      "NAME                        SECRETS  AGE\n",
      "seldon-manager              1        0s\n",
      "seldon-spartakus-volunteer  1        0s\n",
      "\n",
      "==> v1beta1/ClusterRole\n",
      "NAME                        AGE\n",
      "seldon-spartakus-volunteer  0s\n",
      "\n",
      "==> v1beta1/ClusterRoleBinding\n",
      "NAME                        AGE\n",
      "seldon-spartakus-volunteer  0s\n",
      "\n",
      "==> v1beta1/CustomResourceDefinition\n",
      "NAME                                         AGE\n",
      "seldondeployments.machinelearning.seldon.io  0s\n",
      "\n",
      "==> v1beta1/Deployment\n",
      "NAME                        READY  UP-TO-DATE  AVAILABLE  AGE\n",
      "seldon-spartakus-volunteer  0/1    1           0          0s\n",
      "\n",
      "==> v1beta1/MutatingWebhookConfiguration\n",
      "NAME                                   AGE\n",
      "seldon-mutating-webhook-configuration  0s\n",
      "\n",
      "==> v1beta1/ValidatingWebhookConfiguration\n",
      "NAME                                     AGE\n",
      "seldon-validating-webhook-configuration  0s\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install ../helm-charts/seldon-core-operator --name seldon-core  --set usageMetrics.enabled=true   --namespace seldon-system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deployment \"seldon-controller-manager\" successfully rolled out\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/seldon-controller-manager -n seldon-system"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup Ingress\n",
    "Please note: There are reported gRPC issues with ambassador (see https://github.com/SeldonIO/seldon-core/issues/473)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   ambassador\n",
      "LAST DEPLOYED: Fri Sep 27 16:51:01 2019\n",
      "NAMESPACE: seldon\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1/Deployment\n",
      "NAME        READY  UP-TO-DATE  AVAILABLE  AGE\n",
      "ambassador  0/1    1           0          0s\n",
      "\n",
      "==> v1/Pod(related)\n",
      "NAME                         READY  STATUS             RESTARTS  AGE\n",
      "ambassador-5784b5cb9d-xss86  0/1    ContainerCreating  0         0s\n",
      "\n",
      "==> v1/Service\n",
      "NAME              TYPE          CLUSTER-IP    EXTERNAL-IP  PORT(S)                     AGE\n",
      "ambassador        LoadBalancer  10.99.175.44  <pending>    80:32310/TCP,443:30726/TCP  0s\n",
      "ambassador-admin  ClusterIP     10.96.81.219  <none>       8877/TCP                    1s\n",
      "\n",
      "==> v1/ServiceAccount\n",
      "NAME        SECRETS  AGE\n",
      "ambassador  1        1s\n",
      "\n",
      "==> v1beta1/ClusterRole\n",
      "NAME             AGE\n",
      "ambassador       1s\n",
      "ambassador-crds  1s\n",
      "\n",
      "==> v1beta1/ClusterRoleBinding\n",
      "NAME             AGE\n",
      "ambassador       1s\n",
      "ambassador-crds  1s\n",
      "\n",
      "==> v1beta1/CustomResourceDefinition\n",
      "NAME                                          AGE\n",
      "authservices.getambassador.io                 1s\n",
      "consulresolvers.getambassador.io              1s\n",
      "kubernetesendpointresolvers.getambassador.io  1s\n",
      "kubernetesserviceresolvers.getambassador.io   1s\n",
      "mappings.getambassador.io                     1s\n",
      "modules.getambassador.io                      1s\n",
      "ratelimitservices.getambassador.io            1s\n",
      "tcpmappings.getambassador.io                  1s\n",
      "tlscontexts.getambassador.io                  1s\n",
      "tracingservices.getambassador.io              1s\n",
      "\n",
      "\n",
      "NOTES:\n",
      "Congratuations! You've successfully installed Ambassador.\n",
      "\n",
      "For help, visit our Slack at https://d6e.co/slack or view the documentation online at https://www.getambassador.io.\n",
      "\n",
      "To get the IP address of Ambassador, run the following commands:\n",
      "NOTE: It may take a few minutes for the LoadBalancer IP to be available.\n",
      "     You can watch the status of by running 'kubectl get svc -w  --namespace seldon ambassador'\n",
      "\n",
      "  On GKE/Azure:\n",
      "  export SERVICE_IP=$(kubectl get svc --namespace seldon ambassador -o jsonpath='{.status.loadBalancer.ingress[0].ip}')\n",
      "\n",
      "  On AWS:\n",
      "  export SERVICE_IP=$(kubectl get svc --namespace seldon ambassador -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')\n",
      "\n",
      "  echo http://$SERVICE_IP:\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install stable/ambassador --name ambassador --set crds.keep=false --set replicaCount=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Waiting for deployment \"ambassador\" rollout to finish: 0 of 1 updated replicas are available...\n",
      "deployment \"ambassador\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deployment.apps/ambassador"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Port Forward to Ambassador\n",
    "\n",
    "```\n",
    "kubectl port-forward $(kubectl get pods -n seldon -l app.kubernetes.io/name=ambassador -o jsonpath='{.items[0].metadata.name}') -n seldon 8003:8080\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serve Single Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   mymodel\r\n",
      "LAST DEPLOYED: Fri Sep 27 16:54:34 2019\r\n",
      "NAMESPACE: seldon\r\n",
      "STATUS: DEPLOYED\r\n",
      "\r\n",
      "RESOURCES:\r\n",
      "==> v1alpha2/SeldonDeployment\r\n",
      "NAME     AGE\r\n",
      "mymodel  0s\r\n",
      "\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "!helm install ../helm-charts/seldon-single-model --name mymodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\r\n",
      "\u001b[04m\u001b[31;01m#\u001b[39;49;00m \u001b[04m\u001b[31;01mS\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mu\u001b[39;49;00m\u001b[04m\u001b[31;01mr\u001b[39;49;00m\u001b[04m\u001b[31;01mc\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01m:\u001b[39;49;00m \u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mi\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\u001b[04m\u001b[31;01mg\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01mp\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01m.\u001b[39;49;00m\u001b[04m\u001b[31;01mj\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\r\n",
      "{\r\n",
      "    \u001b[34;01m\"apiVersion\"\u001b[39;49;00m: \u001b[33m\"machinelearning.seldon.io/v1alpha2\"\u001b[39;49;00m,\r\n",
      "    \u001b[34;01m\"kind\"\u001b[39;49;00m: \u001b[33m\"SeldonDeployment\"\u001b[39;49;00m,\r\n",
      "    \u001b[34;01m\"metadata\"\u001b[39;49;00m: {\r\n",
      "        \u001b[34;01m\"labels\"\u001b[39;49;00m: {\u001b[34;01m\"app\"\u001b[39;49;00m:\u001b[33m\"seldon\"\u001b[39;49;00m},\r\n",
      "        \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m\r\n",
      "    },\r\n",
      "    \u001b[34;01m\"spec\"\u001b[39;49;00m: {\r\n",
      "        \u001b[34;01m\"annotations\"\u001b[39;49;00m:{\u001b[34;01m\"seldon.io/engine-log-message-type\"\u001b[39;49;00m:\u001b[33m\"seldon.message.pair\"\u001b[39;49;00m,\u001b[34;01m\"seldon.io/engine-log-messages-externally\"\u001b[39;49;00m:\u001b[33m\"false\"\u001b[39;49;00m,\u001b[34;01m\"seldon.io/engine-log-requests\"\u001b[39;49;00m:\u001b[33m\"false\"\u001b[39;49;00m,\u001b[34;01m\"seldon.io/engine-log-responses\"\u001b[39;49;00m:\u001b[33m\"false\"\u001b[39;49;00m,\u001b[34;01m\"seldon.io/headless-svc\"\u001b[39;49;00m:\u001b[33m\"false\"\u001b[39;49;00m},\r\n",
      "        \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m,\r\n",
      "        \u001b[34;01m\"predictors\"\u001b[39;49;00m: [\r\n",
      "            {\r\n",
      "                \u001b[34;01m\"componentSpecs\"\u001b[39;49;00m: [{\r\n",
      "                    \u001b[34;01m\"spec\"\u001b[39;49;00m: {\r\n",
      "                        \u001b[34;01m\"containers\"\u001b[39;49;00m: [\r\n",
      "                            {\r\n",
      "                                \u001b[34;01m\"image\"\u001b[39;49;00m: \u001b[33m\"seldonio/mock_classifier:1.0\"\u001b[39;49;00m,\r\n",
      "                                \u001b[34;01m\"imagePullPolicy\"\u001b[39;49;00m: \u001b[33m\"IfNotPresent\"\u001b[39;49;00m,\r\n",
      "                                \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier\"\u001b[39;49;00m,\r\n",
      "                                \u001b[34;01m\"resources\"\u001b[39;49;00m: {\u001b[34;01m\"requests\"\u001b[39;49;00m:{\u001b[34;01m\"memory\"\u001b[39;49;00m:\u001b[33m\"1Mi\"\u001b[39;49;00m}},\r\n",
      "                                \u001b[34;01m\"env\"\u001b[39;49;00m: [\r\n",
      "{\r\n",
      "\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"LOG_LEVEL\"\u001b[39;49;00m,\r\n",
      "\u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"INFO\"\u001b[39;49;00m\r\n",
      "},\r\n",
      "]\r\n",
      "                            }\r\n",
      "                        ],\r\n",
      "                        \u001b[34;01m\"terminationGracePeriodSeconds\"\u001b[39;49;00m: \u001b[34m1\u001b[39;49;00m\r\n",
      "                    }}\r\n",
      "                                  ],\r\n",
      "                \u001b[34;01m\"graph\"\u001b[39;49;00m:\r\n",
      "                {\r\n",
      "                    \u001b[34;01m\"children\"\u001b[39;49;00m: [],\r\n",
      "                    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier\"\u001b[39;49;00m,\r\n",
      "                    \u001b[34;01m\"type\"\u001b[39;49;00m: \u001b[33m\"MODEL\"\u001b[39;49;00m,\r\n",
      "                    \u001b[34;01m\"endpoint\"\u001b[39;49;00m: {\r\n",
      "                        \u001b[34;01m\"type\"\u001b[39;49;00m: \u001b[33m\"REST\"\u001b[39;49;00m\r\n",
      "                    }},\r\n",
      "                \u001b[34;01m\"svcOrchSpec\"\u001b[39;49;00m: {\r\n",
      "                \u001b[34;01m\"resources\"\u001b[39;49;00m: {\u001b[34;01m\"requests\"\u001b[39;49;00m:{\u001b[34;01m\"cpu\"\u001b[39;49;00m:\u001b[33m\"0.1\"\u001b[39;49;00m}},\r\n",
      "\u001b[34;01m\"env\"\u001b[39;49;00m: [\r\n",
      "]\r\n",
      "                },\r\n",
      "                \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m,\r\n",
      "                \u001b[34;01m\"replicas\"\u001b[39;49;00m: \u001b[34m1\u001b[39;49;00m,\r\n",
      "            \u001b[34;01m\"labels\"\u001b[39;49;00m: {\u001b[34;01m\"fluentd\"\u001b[39;49;00m:\u001b[33m\"true\"\u001b[39;49;00m,\u001b[34;01m\"version\"\u001b[39;49;00m:\u001b[33m\"v1\"\u001b[39;49;00m}\r\n",
      "            }\r\n",
      "        ]\r\n",
      "    }\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!helm template ../helm-charts/seldon-single-model | pygmentize -l json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deployment \"mymodel-mymodel-7cd068f\" successfully rolled out\r\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/mymodel-mymodel-7cd068f"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from seldon_core.seldon_client import SeldonClient\n",
    "sc = SeldonClient(deployment_name=\"mymodel\",namespace=\"seldon\",gateway_endpoint=\"localhost:8003\",gateway=\"ambassador\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### REST Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.269194865761581\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"5nme8lkdp7gvl1js1t93r02l4f\"\n",
      "  requestPath {\n",
      "    key: \"classifier\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.06614500476275062\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(transport=\"rest\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### gRPC Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.06423970175068716\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"8k725959hd75v0m9mquvkd6upc\"\n",
      "  requestPath {\n",
      "    key: \"classifier\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.05455599981648617\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(transport=\"grpc\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"mymodel\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete mymodel --purge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serve AB Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   myabtest\n",
      "LAST DEPLOYED: Wed Aug 14 09:23:40 2019\n",
      "NAMESPACE: seldon\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1alpha2/SeldonDeployment\n",
      "NAME      AGE\n",
      "myabtest  1s\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install ../helm-charts/seldon-abtest --name myabtest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\r\n",
      "\u001b[04m\u001b[31;01m#\u001b[39;49;00m \u001b[04m\u001b[31;01mS\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mu\u001b[39;49;00m\u001b[04m\u001b[31;01mr\u001b[39;49;00m\u001b[04m\u001b[31;01mc\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01m:\u001b[39;49;00m \u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mb\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01mp\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mb\u001b[39;49;00m\u001b[04m\u001b[31;01m_\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01m_\u001b[39;49;00m\u001b[34m1\u001b[39;49;00m\u001b[04m\u001b[31;01mp\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01m.\u001b[39;49;00m\u001b[04m\u001b[31;01mj\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\r\n",
      "\r\n",
      "\r\n",
      "\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\r\n",
      "\u001b[04m\u001b[31;01m#\u001b[39;49;00m \u001b[04m\u001b[31;01mS\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mu\u001b[39;49;00m\u001b[04m\u001b[31;01mr\u001b[39;49;00m\u001b[04m\u001b[31;01mc\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01m:\u001b[39;49;00m \u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mb\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01mp\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mb\u001b[39;49;00m\u001b[04m\u001b[31;01m_\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01m_\u001b[39;49;00m\u001b[34m2\u001b[39;49;00m\u001b[04m\u001b[31;01mp\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01m.\u001b[39;49;00m\u001b[04m\u001b[31;01mj\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\r\n",
      "\r\n",
      "{\r\n",
      "    \u001b[34;01m\"apiVersion\"\u001b[39;49;00m: \u001b[33m\"machinelearning.seldon.io/v1alpha2\"\u001b[39;49;00m,\r\n",
      "    \u001b[34;01m\"kind\"\u001b[39;49;00m: \u001b[33m\"SeldonDeployment\"\u001b[39;49;00m,\r\n",
      "    \u001b[34;01m\"metadata\"\u001b[39;49;00m: {\r\n",
      "\t\u001b[34;01m\"labels\"\u001b[39;49;00m: {\r\n",
      "\t    \u001b[34;01m\"app\"\u001b[39;49;00m: \u001b[33m\"seldon\"\u001b[39;49;00m\r\n",
      "\t},\r\n",
      "\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m\r\n",
      "    },\r\n",
      "    \u001b[34;01m\"spec\"\u001b[39;49;00m: {\r\n",
      "\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m,\r\n",
      "\t\u001b[34;01m\"predictors\"\u001b[39;49;00m: [\r\n",
      "\t    {\r\n",
      "\t\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m,\r\n",
      "\t\t\u001b[34;01m\"replicas\"\u001b[39;49;00m: \u001b[34m1\u001b[39;49;00m,\r\n",
      "\t\t\u001b[34;01m\"componentSpecs\"\u001b[39;49;00m: [{\r\n",
      "\t\t    \u001b[34;01m\"spec\"\u001b[39;49;00m: {\r\n",
      "\t\t\t\u001b[34;01m\"containers\"\u001b[39;49;00m: [\r\n",
      "\t\t\t    {\r\n",
      "\t\t\t\t\u001b[34;01m\"image\"\u001b[39;49;00m: \u001b[33m\"seldonio/mock_classifier:1.0\"\u001b[39;49;00m,\r\n",
      "\t\t\t\t\u001b[34;01m\"imagePullPolicy\"\u001b[39;49;00m: \u001b[33m\"IfNotPresent\"\u001b[39;49;00m,\r\n",
      "\t\t\t\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-1\"\u001b[39;49;00m,\r\n",
      "\t\t\t\t\u001b[34;01m\"resources\"\u001b[39;49;00m: {\r\n",
      "\t\t\t\t    \u001b[34;01m\"requests\"\u001b[39;49;00m: {\r\n",
      "\t\t\t\t\t\u001b[34;01m\"memory\"\u001b[39;49;00m: \u001b[33m\"1Mi\"\u001b[39;49;00m\r\n",
      "\t\t\t\t    }\r\n",
      "\t\t\t\t}\r\n",
      "\t\t\t    }],\r\n",
      "\t\t\t\u001b[34;01m\"terminationGracePeriodSeconds\"\u001b[39;49;00m: \u001b[34m20\u001b[39;49;00m\r\n",
      "\t\t    }},\r\n",
      "\t        {\r\n",
      "\t\t    \u001b[34;01m\"metadata\"\u001b[39;49;00m:{\r\n",
      "\t\t\t\u001b[34;01m\"labels\"\u001b[39;49;00m:{\r\n",
      "\t\t\t    \u001b[34;01m\"version\"\u001b[39;49;00m:\u001b[33m\"v2\"\u001b[39;49;00m\r\n",
      "\t\t\t}\r\n",
      "\t\t    },    \r\n",
      "\t\t\t\u001b[34;01m\"spec\"\u001b[39;49;00m:{\r\n",
      "\t\t\t    \u001b[34;01m\"containers\"\u001b[39;49;00m:[\r\n",
      "\t\t\t    {\r\n",
      "\t\t\t\t\u001b[34;01m\"image\"\u001b[39;49;00m: \u001b[33m\"seldonio/mock_classifier:1.0\"\u001b[39;49;00m,\r\n",
      "\t\t\t\t\u001b[34;01m\"imagePullPolicy\"\u001b[39;49;00m: \u001b[33m\"IfNotPresent\"\u001b[39;49;00m,\r\n",
      "\t\t\t\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-2\"\u001b[39;49;00m,\r\n",
      "\t\t\t\t\u001b[34;01m\"resources\"\u001b[39;49;00m: {\r\n",
      "\t\t\t\t    \u001b[34;01m\"requests\"\u001b[39;49;00m: {\r\n",
      "\t\t\t\t\t\u001b[34;01m\"memory\"\u001b[39;49;00m: \u001b[33m\"1Mi\"\u001b[39;49;00m\r\n",
      "\t\t\t\t    }\r\n",
      "\t\t\t\t}\r\n",
      "\t\t\t    }\r\n",
      "\t\t\t],\r\n",
      "\t\t\t\u001b[34;01m\"terminationGracePeriodSeconds\"\u001b[39;49;00m: \u001b[34m20\u001b[39;49;00m\r\n",
      "\t\t\t\t   }\r\n",
      "\t\t\t\t   }],\r\n",
      "\t\t\u001b[34;01m\"graph\"\u001b[39;49;00m: {\r\n",
      "\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m,\r\n",
      "\t\t    \u001b[34;01m\"endpoint\"\u001b[39;49;00m:{},\r\n",
      "\t\t    \u001b[34;01m\"implementation\"\u001b[39;49;00m:\u001b[33m\"RANDOM_ABTEST\"\u001b[39;49;00m,\r\n",
      "\t\t    \u001b[34;01m\"parameters\"\u001b[39;49;00m: [\r\n",
      "\t\t\t{\r\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m:\u001b[33m\"ratioA\"\u001b[39;49;00m,\r\n",
      "\t\t\t    \u001b[34;01m\"value\"\u001b[39;49;00m:\u001b[33m\"0.5\"\u001b[39;49;00m,\r\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"FLOAT\"\u001b[39;49;00m\r\n",
      "\t\t\t}\r\n",
      "\t\t    ],\r\n",
      "\t\t    \u001b[34;01m\"children\"\u001b[39;49;00m: [\r\n",
      "\t\t\t{\r\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-1\"\u001b[39;49;00m,\r\n",
      "\t\t\t    \u001b[34;01m\"endpoint\"\u001b[39;49;00m:{\r\n",
      "\t\t\t\t\u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"REST\"\u001b[39;49;00m\r\n",
      "\t\t\t    },\r\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"MODEL\"\u001b[39;49;00m,\r\n",
      "\t\t\t    \u001b[34;01m\"children\"\u001b[39;49;00m:[]\r\n",
      "\t\t\t},\r\n",
      "\t\t\t{\r\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-2\"\u001b[39;49;00m,\r\n",
      "\t\t\t    \u001b[34;01m\"endpoint\"\u001b[39;49;00m:{\r\n",
      "\t\t\t\t\u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"REST\"\u001b[39;49;00m\r\n",
      "\t\t\t    },\r\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"MODEL\"\u001b[39;49;00m,\r\n",
      "\t\t\t    \u001b[34;01m\"children\"\u001b[39;49;00m:[]\r\n",
      "\t\t\t}   \r\n",
      "\t\t    ]\r\n",
      "\t\t}\r\n",
      "\t    }\r\n",
      "\t]\r\n",
      "    }\r\n",
      "}\t\t\r\n",
      "\t\t\r\n",
      "\r\n",
      " \r\n"
     ]
    }
   ],
   "source": [
    "!helm template ../helm-charts/seldon-abtest | pygmentize -l json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deployment \"myabtest-myabtest-41de5b8\" successfully rolled out\n",
      "deployment \"myabtest-myabtest-df66c5c\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/myabtest-myabtest-41de5b8\n",
    "!kubectl rollout status deploy/myabtest-myabtest-df66c5c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "from seldon_core.seldon_client import SeldonClient\n",
    "sc = SeldonClient(deployment_name=\"myabtest\",namespace=\"seldon\",gateway_endpoint=\"localhost:8003\",gateway=\"ambassador\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### REST Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.7243037145484317\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"jkbbad2ck5572fuq3m97hf1jnb\"\n",
      "  routing {\n",
      "    key: \"myabtest\"\n",
      "    value: 1\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-2\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"myabtest\"\n",
      "    value: \"\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.10043839793672894\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(transport=\"rest\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### gRPC Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.11690007088816501\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"4h2r8fn7hcu1o3lldhlrus1rns\"\n",
      "  routing {\n",
      "    key: \"myabtest\"\n",
      "    value: 0\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-1\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"myabtest\"\n",
      "    value: \"\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.057336789926229086\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(transport=\"grpc\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"myabtest\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete myabtest --purge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serve Multi-Armed Bandit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME:   mymab\n",
      "LAST DEPLOYED: Wed Aug 14 09:24:49 2019\n",
      "NAMESPACE: seldon\n",
      "STATUS: DEPLOYED\n",
      "\n",
      "RESOURCES:\n",
      "==> v1alpha2/SeldonDeployment\n",
      "NAME   AGE\n",
      "mymab  0s\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!helm install ../helm-charts/seldon-mab --name mymab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\n",
      "\u001b[04m\u001b[31;01m#\u001b[39;49;00m \u001b[04m\u001b[31;01mS\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mu\u001b[39;49;00m\u001b[04m\u001b[31;01mr\u001b[39;49;00m\u001b[04m\u001b[31;01mc\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01m:\u001b[39;49;00m \u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01md\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\u001b[04m\u001b[31;01m-\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mb\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01mp\u001b[39;49;00m\u001b[04m\u001b[31;01ml\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mt\u001b[39;49;00m\u001b[04m\u001b[31;01me\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01m/\u001b[39;49;00m\u001b[04m\u001b[31;01mm\u001b[39;49;00m\u001b[04m\u001b[31;01ma\u001b[39;49;00m\u001b[04m\u001b[31;01mb\u001b[39;49;00m\u001b[04m\u001b[31;01m.\u001b[39;49;00m\u001b[04m\u001b[31;01mj\u001b[39;49;00m\u001b[04m\u001b[31;01ms\u001b[39;49;00m\u001b[04m\u001b[31;01mo\u001b[39;49;00m\u001b[04m\u001b[31;01mn\u001b[39;49;00m\n",
      "{\n",
      "    \u001b[34;01m\"apiVersion\"\u001b[39;49;00m: \u001b[33m\"machinelearning.seldon.io/v1alpha2\"\u001b[39;49;00m,\n",
      "    \u001b[34;01m\"kind\"\u001b[39;49;00m: \u001b[33m\"SeldonDeployment\"\u001b[39;49;00m,\n",
      "    \u001b[34;01m\"metadata\"\u001b[39;49;00m: {\n",
      "\t\t\u001b[34;01m\"labels\"\u001b[39;49;00m: {\u001b[34;01m\"app\"\u001b[39;49;00m:\u001b[33m\"seldon\"\u001b[39;49;00m},\n",
      "\t\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m\n",
      "    },\n",
      "    \u001b[34;01m\"spec\"\u001b[39;49;00m: {\n",
      "\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m,\n",
      "\t\u001b[34;01m\"predictors\"\u001b[39;49;00m: [\n",
      "\t    {\n",
      "\t\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"release-name\"\u001b[39;49;00m,\n",
      "\t\t\u001b[34;01m\"replicas\"\u001b[39;49;00m: \u001b[34m1\u001b[39;49;00m,\n",
      "\t\t\u001b[34;01m\"componentSpecs\"\u001b[39;49;00m: [{\n",
      "\t\t    \u001b[34;01m\"spec\"\u001b[39;49;00m: {\n",
      "\t\t\t\u001b[34;01m\"containers\"\u001b[39;49;00m: [\n",
      "\t\t\t    {\n",
      "\t\t\t\t\u001b[34;01m\"image\"\u001b[39;49;00m: \u001b[33m\"seldonio/mock_classifier:1.0\"\u001b[39;49;00m,\n",
      "\t\t\t\t\u001b[34;01m\"imagePullPolicy\"\u001b[39;49;00m: \u001b[33m\"IfNotPresent\"\u001b[39;49;00m,\n",
      "\t\t\t\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-1\"\u001b[39;49;00m,\n",
      "\t\t\t\t\u001b[34;01m\"resources\"\u001b[39;49;00m: {\n",
      "\t\t\t\t    \u001b[34;01m\"requests\"\u001b[39;49;00m: {\n",
      "\t\t\t\t\t\u001b[34;01m\"memory\"\u001b[39;49;00m: \u001b[33m\"1Mi\"\u001b[39;49;00m\n",
      "\t\t\t\t    }\n",
      "\t\t\t\t}\n",
      "\t\t\t    }],\n",
      "\t\t\t\u001b[34;01m\"terminationGracePeriodSeconds\"\u001b[39;49;00m: \u001b[34m20\u001b[39;49;00m\n",
      "\t\t    }},\n",
      "\t        {\n",
      "\t\t\t\u001b[34;01m\"spec\"\u001b[39;49;00m:{\n",
      "\t\t\t    \u001b[34;01m\"containers\"\u001b[39;49;00m:[\n",
      "\t\t\t    {\n",
      "\t\t\t\t\u001b[34;01m\"image\"\u001b[39;49;00m: \u001b[33m\"seldonio/mock_classifier:1.0\"\u001b[39;49;00m,\n",
      "\t\t\t\t\u001b[34;01m\"imagePullPolicy\"\u001b[39;49;00m: \u001b[33m\"IfNotPresent\"\u001b[39;49;00m,\n",
      "\t\t\t\t\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-2\"\u001b[39;49;00m,\n",
      "\t\t\t\t\u001b[34;01m\"resources\"\u001b[39;49;00m: {\n",
      "\t\t\t\t    \u001b[34;01m\"requests\"\u001b[39;49;00m: {\n",
      "\t\t\t\t\t\u001b[34;01m\"memory\"\u001b[39;49;00m: \u001b[33m\"1Mi\"\u001b[39;49;00m\n",
      "\t\t\t\t    }\n",
      "\t\t\t\t}\n",
      "\t\t\t    }\n",
      "\t\t\t],\n",
      "\t\t\t\u001b[34;01m\"terminationGracePeriodSeconds\"\u001b[39;49;00m: \u001b[34m20\u001b[39;49;00m\n",
      "\t\t\t}\n",
      "\t\t},\n",
      "\t        {\n",
      "\t\t    \u001b[34;01m\"spec\"\u001b[39;49;00m:{\n",
      "\t\t\t\u001b[34;01m\"containers\"\u001b[39;49;00m: [{\n",
      "\t\t\t    \u001b[34;01m\"image\"\u001b[39;49;00m: \u001b[33m\"seldonio/mab_epsilon_greedy:1.1\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"eg-router\"\u001b[39;49;00m\n",
      "\t\t\t}],\n",
      "\t\t\t\u001b[34;01m\"terminationGracePeriodSeconds\"\u001b[39;49;00m: \u001b[34m20\u001b[39;49;00m\n",
      "\t\t    }}\n",
      "\t        ],\n",
      "\t\t\u001b[34;01m\"graph\"\u001b[39;49;00m: {\n",
      "\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"eg-router\"\u001b[39;49;00m,\n",
      "\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"ROUTER\"\u001b[39;49;00m,\n",
      "\t\t    \u001b[34;01m\"parameters\"\u001b[39;49;00m: [\n",
      "\t\t\t{\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"n_branches\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"2\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m: \u001b[33m\"INT\"\u001b[39;49;00m\n",
      "\t\t\t},\n",
      "\t\t\t{\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"epsilon\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"0.2\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m: \u001b[33m\"FLOAT\"\u001b[39;49;00m\n",
      "\t\t\t},\n",
      "\t\t\t{\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"verbose\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"1\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m: \u001b[33m\"BOOL\"\u001b[39;49;00m\n",
      "\t\t\t}\n",
      "\t\t    ],\n",
      "\t\t    \u001b[34;01m\"children\"\u001b[39;49;00m: [\n",
      "\t\t\t{\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-1\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"endpoint\"\u001b[39;49;00m:{\n",
      "\t\t\t\t\u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"REST\"\u001b[39;49;00m\n",
      "\t\t\t    },\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"MODEL\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"children\"\u001b[39;49;00m:[]\n",
      "\t\t\t},\n",
      "\t\t\t{\n",
      "\t\t\t    \u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"classifier-2\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"endpoint\"\u001b[39;49;00m:{\n",
      "\t\t\t\t\u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"REST\"\u001b[39;49;00m\n",
      "\t\t\t    },\n",
      "\t\t\t    \u001b[34;01m\"type\"\u001b[39;49;00m:\u001b[33m\"MODEL\"\u001b[39;49;00m,\n",
      "\t\t\t    \u001b[34;01m\"children\"\u001b[39;49;00m:[]\n",
      "\t\t\t}   \n",
      "\t\t    ]\n",
      "\t\t},\n",
      "\t\t\u001b[34;01m\"svcOrchSpec\"\u001b[39;49;00m: {\n",
      "\t\t\u001b[34;01m\"resources\"\u001b[39;49;00m: {\u001b[34;01m\"requests\"\u001b[39;49;00m:{\u001b[34;01m\"cpu\"\u001b[39;49;00m:\u001b[33m\"0.1\"\u001b[39;49;00m}},\n",
      "\u001b[34;01m\"env\"\u001b[39;49;00m: [\n",
      "{\n",
      "\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"SELDON_LOG_MESSAGES_EXTERNALLY\"\u001b[39;49;00m,\n",
      "\u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"false\"\u001b[39;49;00m\n",
      "},\n",
      "{\n",
      "\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"SELDON_LOG_MESSAGE_TYPE\"\u001b[39;49;00m,\n",
      "\u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"seldon.message.pair\"\u001b[39;49;00m\n",
      "},\n",
      "{\n",
      "\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"SELDON_LOG_REQUESTS\"\u001b[39;49;00m,\n",
      "\u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"false\"\u001b[39;49;00m\n",
      "},\n",
      "{\n",
      "\u001b[34;01m\"name\"\u001b[39;49;00m: \u001b[33m\"SELDON_LOG_RESPONSES\"\u001b[39;49;00m,\n",
      "\u001b[34;01m\"value\"\u001b[39;49;00m: \u001b[33m\"false\"\u001b[39;49;00m\n",
      "},\n",
      "]\n",
      "},\n",
      "\t\t\u001b[34;01m\"labels\"\u001b[39;49;00m: {\u001b[34;01m\"fluentd\"\u001b[39;49;00m:\u001b[33m\"true\"\u001b[39;49;00m,\u001b[34;01m\"version\"\u001b[39;49;00m:\u001b[33m\"v1\"\u001b[39;49;00m}\n",
      "\t    }\n",
      "\t]\n",
      "    }\n",
      "}\n",
      "\t\t\n",
      "\n",
      " \n"
     ]
    }
   ],
   "source": [
    "!helm template ../helm-charts/seldon-mab | pygmentize -l json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "deployment \"mymab-mymab-41de5b8\" successfully rolled out\n",
      "deployment \"mymab-mymab-b8038b2\" successfully rolled out\n",
      "deployment \"mymab-mymab-df66c5c\" successfully rolled out\n"
     ]
    }
   ],
   "source": [
    "!kubectl rollout status deploy/mymab-mymab-41de5b8\n",
    "!kubectl rollout status deploy/mymab-mymab-b8038b2\n",
    "!kubectl rollout status deploy/mymab-mymab-df66c5c "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "from seldon_core.seldon_client import SeldonClient\n",
    "sc = SeldonClient(deployment_name=\"mymab\",namespace=\"seldon\",gateway_endpoint=\"localhost:8003\",gateway=\"ambassador\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### REST Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.41418976743464186\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"930tive9mqkli5dk6d2odnssu8\"\n",
      "  routing {\n",
      "    key: \"eg-router\"\n",
      "    value: 1\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-2\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"eg-router\"\n",
      "    value: \"seldonio/mab_epsilon_greedy:1.1\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.07568472249519131\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(transport=\"rest\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### gRPC Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success:True message:\n",
      "Request:\n",
      "data {\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.5241212703444827\n",
      "  }\n",
      "}\n",
      "\n",
      "Response:\n",
      "meta {\n",
      "  puid: \"p92hk3957rl0uieuss0ucia2bg\"\n",
      "  routing {\n",
      "    key: \"eg-router\"\n",
      "    value: 0\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"classifier-1\"\n",
      "    value: \"seldonio/mock_classifier:1.0\"\n",
      "  }\n",
      "  requestPath {\n",
      "    key: \"eg-router\"\n",
      "    value: \"seldonio/mab_epsilon_greedy:1.1\"\n",
      "  }\n",
      "}\n",
      "data {\n",
      "  names: \"proba\"\n",
      "  tensor {\n",
      "    shape: 1\n",
      "    shape: 1\n",
      "    values: 0.08374291648417304\n",
      "  }\n",
      "}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = sc.predict(transport=\"grpc\")\n",
    "print(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "release \"mymab\" deleted\r\n"
     ]
    }
   ],
   "source": [
    "!helm delete mymab --purge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
